<?php
namespace App\Http\Controllers\Api\v1\Astro;
use App\Http\Controllers\Api\BaseController;
use Illuminate\Support\Facades\Input;
use Illuminate\Http\Request;



//
class IndexController extends BaseController
{

   /* public function __construct()
    {
        parent::filter_xss();
        if(!defined('IS_ACCESS_TOKEN_CHECKED'))
        {
            parent::__construct();// 接口调用验证
        }
    }*/


    public function getTheBaseInfo($year=null,$month = null,$day=null,$hour=null,$minute = null,
                                   $longitude=null,$latitude=null,$timezone=8,$h_sys='p')
    {
        $year = Request::input('year');
        $month = Request::input('month');
        $day = Request::input('day');
        $hour = Request::input('hour');
        $minute = Request::input('minute');
        $longitude = Request::input('longitude');
        $latitude = Request::input('latitude');
//        $timezone = Input::get('timezone');
//        $h_sys = Input::get('h_sys');


        $time = $this->getTheBaseTime($year, $month, $day, $hour, $minute, $timezone);
        $utdatenow = strftime("%d.%m.%Y", $time);
        $utnow = strftime("%H:%M:%S", $time);

        exec (config('astrolog.prefix')." -b{$utdatenow} -ut{$utnow} -p0123456789DAmHt -eswe -house{$longitude},{$latitude},{$h_sys} -flds -g, -head", $out);
        if(!empty($out) || count($out) < 27)
        {
            unset($out);
            exec (config('astrolog.prefix')." -b{$utdatenow} -ut{$utnow} -p0123456789DAmHt -eswe -house{$longitude},{$latitude},w -flds -g, -head", $out);
        }
        $data = $this->getOriginalData($out,$h_sys);

        return response()->json($data);
    }
    /**获取档案星盘数据
     * @param null $year
     * @param null $month
     * @param null $day
     * @param null $hour
     * @param null $minute
     * @param null $longitude
     * @param null $latitude
     * @param int $timezone
     * @param string $h_sys
     * @return array
     */

    public function getTheRecodeAstro($year=null,$month = null,$day=null,$hour=null,$minute = null,
                                      $longitude=null,$latitude=null,$timezone=8,$h_sys='p')
    {
        $time = $this->getTheBaseTime($year, $month, $day, $hour, $minute, $timezone);
        $utdatenow = strftime("%d.%m.%Y", $time);
        $utnow = strftime("%H:%M:%S", $time);

        exec (config('astrolog.prefix')." -b{$utdatenow} -ut{$utnow} -p0123456789DAmHt -eswe -house{$longitude},{$latitude},{$h_sys} -flds -g, -head", $out);

        $data = $this->getOriginalData($out);

        $result = [];
        $result['sun'] = InWhichSign($data['p1'][0]).'-'.InWhichHouse($data['p1'][0],$data['hc1']).'-'.$data['p1'][0];
        $result['moon'] = InWhichSign($data['p1'][1]).'-'.InWhichHouse($data['p1'][1],$data['hc1']).'-'.$data['p1'][1];
        $result['merc'] = InWhichSign($data['p1'][2]).'-'.InWhichHouse($data['p1'][2],$data['hc1']).'-'.$data['p1'][2];
        $result['venu'] = InWhichSign($data['p1'][3]).'-'.InWhichHouse($data['p1'][3],$data['hc1']).'-'.$data['p1'][3];
        $result['mars'] = InWhichSign($data['p1'][4]).'-'.InWhichHouse($data['p1'][4],$data['hc1']).'-'.$data['p1'][4];
        $result['jupi'] = InWhichSign($data['p1'][5]).'-'.InWhichHouse($data['p1'][5],$data['hc1']).'-'.$data['p1'][5];
        $result['satu'] = InWhichSign($data['p1'][6]).'-'.InWhichHouse($data['p1'][6],$data['hc1']).'-'.$data['p1'][6];
        $result['non'] = InWhichSign($data['p1'][12]).'-'.InWhichHouse($data['p1'][12],$data['hc1']).'-'.$data['p1'][12];
        $result['lili'] = InWhichSign($data['p1'][11]).'-'.InWhichHouse($data['p1'][11],$data['hc1']).'-'.$data['p1'][11];
        $result['juno'] = InWhichSign($data['p1'][13]).'-'.InWhichHouse($data['p1'][13],$data['hc1']).'-'.$data['p1'][13];
        $result['asc'] = InWhichSign($data['p1'][17]).'-'.InWhichHouse($data['p1'][17],$data['hc1']).'-'.$data['p1'][17];
        $result['mc'] = InWhichSign($data['p1'][20]).'-'.InWhichHouse($data['p1'][20],$data['hc1']).'-'.$data['p1'][20];

        return $result;
    }
    /**初始化本命盘
     * @param array $data
     * @param string $h_sys
     * @return array
     */

    public function getOriginalData($data = [],$h_sys = 'p')
    {
        $planet = [];
        $speed  = [];
        foreach ($data as $key => $line)
        {
            $row = explode(',',$line);
            //$planet[$key] = $row[0]*1;
            //$speed[$key] = $row[1]*1;
            $planet[$key] = @floatval($row[0])*1;
            $declination[$key] = isset($row[1]) ? floatval($row[1]) *1 : 0;
            $speed[$key] = isset($row[2]) ? floatval($row[2])*1 : 0;
        };

        // 判断是日间盘还是夜间盘
        if ($planet[15] > $planet[21])
            if ($planet[0] <= $planet[15] And $planet[0] > $planet[21])
                $day_chart = true;
            else
                $day_chart = false;
        else
            if ($planet[0] > $planet[15] And $planet[0] <= $planet[21])
                $day_chart = false;
            else
                $day_chart = true;

        $juno = $planet[13];// 婚神星
        // 计算福点位置
        if ($day_chart == True) $fort = $planet[15] + $planet[1] - $planet[0];
        else $fort = $planet[15] - $planet[1] + $planet[0];
        if ($fort >= 360) $fort -= 360;
        if ($fort < 0) $fort += 360;

        // 计算宿命点度数
        $vert = $planet[30];
        $asc = $planet[27];
        $mc = $planet[28];
        $des = $asc >= 180 ? $asc - 180 : $asc + 180;
        $ic = $mc >= 180 ? $mc - 180 : $mc + 180;
        $hc = [];
        // 此处 加入特殊分宫制   整宫制
        /*if($h_sys == 'w')
        {
            $hc[1] = floor($planet[15]/30) *30;
            for ($i=1;$i <= 14; $i++)
            {
                $house = $hc[1] + ($i-1)*30;
                if($house > 360)
                {
                    $house -=360;
                }
                $hc[$i] = $house;
                unset($planet[14 + $i]);
            }
        }
        else
        {*/
       for ($i = 1; $i <= 14; $i++)
        {
                $hc[$i] = $planet[14 + $i];
                unset($planet[14 + $i]);
            }
        //}
        unset($hc[13]);unset($hc[14]);
        unset($planet[29]);unset($planet[30]);

        $planet[13] = $juno;// 婚神星
        $planet[14] = $fort;// 福点
        $planet[15] = $vert;// 宿命点
        $planet[16] = $planet[12] > 180 ? $planet[12] - 180 : $planet[12] + 180;// 增加南交点
        // 加四轴 放入行星组中
        $planet[17] = $asc ;// 上升点
        $planet[18] = $ic;// 天底
        $planet[19] = $des;// 下降点
        $planet[20] = $mc;// 天顶


        $retro = $this->getRetroData($speed);

        $returnData = ['rx1'=>$retro, 'p1'=>$planet, 'hc1'=>$hc, 'speed'=>$speed];

        return $returnData;
    }

    /**
     * 获取是否逆行数据
     * @param  [type] $speed [description]
     * @return [type]        [description]
     */
    protected function getRetroData($speed)
    {
        $retro = '';

        for ($i = 0; $i <= 13; $i++)
        {
            if (@$speed[$i] < 0) $retro .= "R";
            else $retro .= " ";
        }

        return $retro;
    }

    /**
     * 获取基础时间
     * @param null $year
     * @param null $month
     * @param null $day
     * @param null $hour
     * @param null $minute
     * @param int $timezone
     * @return false|int
     */
    private function getTheBaseTime($year=null,$month = null,$day=null,$hour=null,$minute = null,$timezone=8)
    {
        if(isSummerTime($hour, $minute, 0, $month, $day, $year))
        {
            $hour --;// 夏令时减一小时
        }

        if ($timezone >= 0)
        {
            $whole = floor($timezone);
            $fraction = $timezone - floor($timezone);
        }
        else
        {
            $whole = ceil($timezone);
            $fraction = $timezone - ceil($timezone);
        }
        $hour = $hour - $whole;
        $minute= $minute- ($fraction * 60);

        $time = mktime($hour, $minute, 0, $month, $day, $year);

        return $time;
    }


}
